<%- include('css') %>
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>类别编辑</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input type="number" name="num" lay-verify="required" placeholder="请输入类别序号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" placeholder="请输入类别名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">封面图</label>
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="headpic">上传图片</button>
                    <div class="layui-upload-list">
                        <img v-if="haspic" style="width: 92px; height: 92px; margin: 0 10px 10px 0;" id="headpreview"
                            v-bind:src="pic">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">保存</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>

<%- include('script') %>

<script>
    var vm = new Vue({
        el: '#app',
        data:function(){
            return {
                isButtonDisabled:false,
                pic:"",
                haspic:false,
                id:'<%=id%>',
                // load:false
            }
        },
        mounted:function(){
            var self = this;
            layui.use(['form', 'upload'], function () {
                var form = layui.form;
                var upload = layui.upload;

                var load_1 = layer.load()
                $.ajax({
                    url:'/admin/type/'+self.id,
                    type:'GET',
                    data:{},
                    success:(res)=>{
                        if(res.code === 200){
                            // let inter = setInterval(()=>{
                            //     if(self.load){
                                    form.val("form",res.data);
                                    self.haspic = true;
                                    self.pic = res.data.pic;
                                    // clearInterval(inter);
                                    layer.close(load_1);
                                // }
                            // },1000)
                        }else{
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            parent.layer.msg('获取数据失败：'+res.error+' -- '+res.code,{icon:2});
                        }
                    },
                    fail:()=>{
                        layer.close(l);
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.layer.msg('请求获取数据失败',{icon:2});
                    }
                });

                //执行实例
                var uploadInst = upload.render({
                    elem: '#headpic' //绑定元素
                        ,
                    url: '/common/upload/zipimg' //上传接口
                        ,
                    before: function (obj) {
                        //预读本地文件示例，不支持ie8
                        obj.preview(function (index, file, result) {
                            self.haspic = true
                            self.pic = result
                        });
                    },
                    done: function (res) {
                        //如果上传失败
                        if (res.code > 0) {
                            self.haspic = false;
                            self.pic = '';
                            return layer.msg('上传失败',{icon:2});
                        } else {
                            self.pic =  res.url
                        }
                        //上传成功
                    },
                    error: function () {
                    }
                });

                //监听提交
                form.on('submit(save)', function (data) {
                    data.field.pic = self.pic//lodash.replace(self.headpic, baseset.server_url, '')
                    self.isButtonDisabled = true;
                    $.ajax({
                        url:'/admin/type/'+self.id,
                        type:'PUT',
                        data:{data:data.field},
                        success:(res)=>{
                            if(res.code === 200){
                                parent.vm.iframesave()
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.layer.msg('修改成功!',{icon:1})
                            }else{
                                layer.msg('修改失败:'+res.error+' -- '+res.code,{icon:2})
                            }
                            self.isButtonDisabled = false;
                        }
                    });
                    return false;
                });
            });
        },
        methods:{
            doclose:function(){
                parent.vm.iframeclose();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            }
        }
    })
</script>
